package com.ada.spark.sparksql

import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{Dataset, SparkSession}


object SparkSql05_UDF {

    def main(args: Array[String]) {

        //创建配置对象
        val conf: SparkConf = new SparkConf().setAppName("SparkSql05_UDF").setMaster("local[*]")

        //创建SparkSql的环境对象
        val spark: SparkSession = SparkSession.builder().config(conf).getOrCreate();

        //进行转换之前，需要引入隐式转换规则
        //这里的spark不是包名的含义，是SparkSession对象的名字
        import spark.implicits._

        //创建RDD
        val rdd: RDD[(Int, String, Int)] = spark.sparkContext.makeRDD(List((1, "Jack", 20), (2, "Bob", 22), (3, "Lucy", 28)))

        //RDD -> DataSet
        val userRDD: RDD[User] = rdd.map {
            case (id, name, age) => {
                User(id, name, age)
            }
        }

        val userDS: Dataset[User] = userRDD.toDS()

        //创建临时视图
        userDS.createOrReplaceTempView("user")

        //注册自定义函数
        spark.udf.register("addName", (x: String) => "Name:" + x)

        //使用自定义函数
        spark.sql("Select addName(name), age from user").show()

        //释放资源
        spark.stop()

    }

}


